package com.aclass.wang.c206;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.util.HashMap;
import java.util.Map;

public class Main9Activity extends AppCompatActivity {
//文本控制与第一阶段
    Map<Integer, Class> mTestItems = new HashMap<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main9);
        //mTestItems.put(R.id.btn_test8,Main8Activity.class);
        Button startNormalActivity = (Button) findViewById(R.id.btn_test3);
        startNormalActivity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Main9Activity.this, Main2Activity.class);
                startActivity(intent);
            }
        });

        Button startMain3Activity = (Button) findViewById(R.id.btn_test4);
        startMain3Activity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Main9Activity.this, Main3Activity.class);
                startActivity(intent);
            }
        });

        final EditText edit1 = (EditText) findViewById(R.id.edit_test1);
        final TextView text2 = (TextView) findViewById(R.id.text2);
        Button btnRed = (Button) findViewById(R.id.btn_red);
        Button btnGreen = (Button) findViewById(R.id.btn_green);
        Button btn1 = (Button) findViewById(R.id.btn_test1);
        Button btn2 = (Button) findViewById(R.id.btn_test2);

        TextView text1 = (TextView) findViewById(R.id.text1);
        //利用findViewById方法找到对应视图

        String strFromText1 = text1.getText().toString();
        text2.setText("文本一的内容是" + strFromText1);

        //所谓监听器，是指一个随时监听特定事件发生的对象，当它所监听的事件发生时，监听器对象中对应的方法就会得到调用（捕获特定事件），从而执行处理事件发生的代码（事件处理）。

        View.OnClickListener listener = new View.OnClickListener() {
            int count;

            @Override
            public void onClick(View v) {
                if (count == 0) {
                    text2.setText("请点我");
                } else {
                    text2.setText("你点了我" + count + "次了");
                }
                count++;
            }
        };

        edit1.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String textFromEditText = s.toString();
                text2.setText("文本框一说：" + textFromEditText);
            }
        });

        btn1.setOnClickListener(listener);
        btn1.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                Toast.makeText(Main9Activity.this, "你长按了我",
                        Toast.LENGTH_LONG).show();
                return false;
            }
        });

        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String textFromEditText = edit1.getText().toString();
                Toast.makeText(Main9Activity.this, "文本框1说：" + textFromEditText,
                        Toast.LENGTH_LONG).show();
            }
        });


        btnRed.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //#rgb #rrggbb #aarrggbb  a:透明度
                text2.setTextColor(0xFFFF0000);
            }
        });

        btnGreen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //#rgb #rrggbb #aarrggbb  a:透明度
                text2.setTextColor(0xFFFF8888);
            }
        });
    }

    public void onClick(View view) {
        Class dst = mTestItems.get(view.getId());
        Intent intent = new Intent(this, dst);
        startActivity(intent);
    }
}
